package cn.byapps.hw.service.impl;

import cn.byapps.hw.dao.HomeworkDao;
import cn.byapps.hw.dao.StudentDao;
import cn.byapps.hw.dto.HomeWorkDTO;
import cn.byapps.hw.entity.Clazz;
import cn.byapps.hw.entity.Student;
import cn.byapps.hw.service.HomeworkService;
import cn.byapps.hw.vo.AssignmentData;
import cn.byapps.hw.vo.HomeworkDetailVO;
import cn.byapps.hw.vo.HomeworkUploadDetailVo;
import cn.byapps.hw.vo.HomeworkVueDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class HomeworkServiceImpl implements HomeworkService {

    @Resource
    private HomeworkDao homeworkDao;
    @Resource
    private StudentDao studentDao;

    @Override
    public List<HomeworkVueDate> homeworkVueDates(Integer clazzId, Integer courseId) {
        /**
         * 获取班级所有学生
         * 根据班级id查询
         */
        List<Student> students = studentDao.findPageByClazz(null, new Clazz(clazzId, null, null));
        /**
         * 获取该课程布置了多少门作业
         * 根据班级id和课程id查询
         */
        List<HomeworkDetailVO> homeworkDetailVOS = new ArrayList<>();
        for (HomeWorkDTO obj : homeworkDao.getHomeWorkDTO(clazzId, courseId)) {
            homeworkDetailVOS.add(new HomeworkDetailVO(obj.getId(), obj.getName(), Integer.parseInt(String.valueOf(obj.getCount()))));
        }
        /**
         * 查询该班级该课程所有学生的所有作业提交情况
         * 根据班级id和课程id查询
         */
        List<HomeworkUploadDetailVo> homeworkUploadDetailVos = new ArrayList<>();
        for (Object[] obj : homeworkDao.getAllHomeWorkFileDTO(clazzId, courseId)) {
            homeworkUploadDetailVos.add(new HomeworkUploadDetailVo((String) obj[0], (String) obj[1], Integer.valueOf(obj[2].toString())));
        }

        List<AssignmentData> assignmentData = new ArrayList<>();

        /**
         * 循环每一个学生
         */
        for (Student student : students) {
            /**
             * 定义一个集合用来存储学生是否提交作业
             */
            List<Boolean> isUpload = new ArrayList<>();
            /**
             * 循环每一个作业
             */
            for (HomeworkDetailVO h : homeworkDetailVOS) {
                /**
                 * 默认学生没有上交作业
                 */
                boolean b = false;

                /**
                 * 根据作业id筛选所有提交内容
                 */
                List<HomeworkUploadDetailVo> hud = new ArrayList<>();
                for (HomeworkUploadDetailVo hup : homeworkUploadDetailVos) {
                    if (hup.getHomeworkId().equals(h.getId())) {
                        hud.add(hup);
                    }
                }
                /**
                 * 根据删选后的作业提取情况,判断学生是否提交
                 */
                for (HomeworkUploadDetailVo hd : hud) {
                    if (hd.getStudentName().equals(student.getName())) {
                        b = true;
                    }
                }
                if (b) {
                    isUpload.add(true);
                } else {
                    isUpload.add(false);
                }
            }
            /**
             * 判断学生提交作业的次数
             */
            int num = 0;
            for (Boolean b : isUpload) {
                if (b == true) {
                    num++;
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("isUplod", isUpload);    //存储是否提交作业
            map.put("total", num);           //存储提交作业的数量
            if (num == 0) {                  //存储平时成绩，如果提交次数为0，则0分处理
                map.put("mark", 0);
            } else {
                map.put("mark", Math.round((double) num / isUpload.size() * 100));
            }
            assignmentData.add(new AssignmentData(student.getName(), map));
        }
        List<HomeworkVueDate> homeworkVueDates = new ArrayList<>();
        homeworkVueDates.add(new HomeworkVueDate("ok", homeworkDetailVOS, assignmentData));
        return homeworkVueDates;
    }
}
